package com.yanming.demo.core.advice;

import com.yanming.demo.core.response.RestResult;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@RestControllerAdvice
public class RestResponseAdviceExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public RestResult<String> handle(Exception ex) {
        return new RestResult(-1,"异常",ex.getMessage());
    }
    
    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public RestResult<String>  handle(RuntimeException ex) {
        return new RestResult(-1,"允许时异常",ex.getMessage());
    }



    //空指针异常
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public RestResult<String>  handle(NullPointerException ex) {
        return new RestResult(-1,"空指针异常",ex.getMessage());
    }
    //类型转换异常
    @ExceptionHandler(ClassCastException.class)
    @ResponseBody
    public RestResult<String>  handle(ClassCastException ex) {
        return new RestResult(-1,"类型转换异常",ex.getMessage());
    }

    //IO异常
    @ExceptionHandler(IOException.class)
    @ResponseBody
    public RestResult<String>  handle(IOException ex) {
        return new RestResult(-1,"IO异常",ex.getMessage());
    }
    //未知方法异常
    @ExceptionHandler(NoSuchMethodException.class)
    @ResponseBody
    public RestResult<String>  handle(NoSuchMethodException ex) {
           return new RestResult(-1,"未知方法异常",ex.getMessage());
    }

    //数组越界异常
    @ExceptionHandler(IndexOutOfBoundsException.class)
    @ResponseBody
    public RestResult<String>  handle(IndexOutOfBoundsException ex) {
           return new RestResult(-1,"数组越界异常",ex.getMessage());
    }
    //400错误
    @ExceptionHandler({HttpMessageNotReadableException.class,TypeMismatchException.class,MissingServletRequestParameterException.class})
    @ResponseBody
    public RestResult<String>  request400(Exception ex){
           return new RestResult(-1,"400错误",ex.getMessage());
    }

    //
    //405错误
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    @ResponseBody
    public RestResult<String>  request405(HttpRequestMethodNotSupportedException ex){
           return new RestResult(-1,"405错误",ex.getMessage());
    }
    //406错误
    @ExceptionHandler({HttpMediaTypeNotAcceptableException.class})
    @ResponseBody
    public RestResult<String>  request406(HttpMediaTypeNotAcceptableException ex){
           return new RestResult(-1,"406错误",ex.getMessage());
    }
    //500错误
    @ExceptionHandler({ConversionNotSupportedException.class,HttpMessageNotWritableException.class})
    @ResponseBody
    public RestResult<String>  request500(Exception ex){
           return new RestResult(-1,"500错误",ex.getMessage());
    }

   
}
